//
//  HomeProvider.swift
//  Alamofire
//
//  Created by Daiyi on 2024/4/21.
//

import DDBase
import SwifterSwift
import KPAppKit


enum HomeNetType {
    static let base = 1300
    
    case getProxy(_ params: [String: Any] = [:])
    case getProducts(_ params: [String: Any] = [:])
    case getAutoPayInfo(_ params: [String: Any] = [:])
    case getOrderInfo
    case createTmpOrder(_ params: [String: Any] = [:])
    case removeTmpOrder(_ params: [String: Any] = [:])
    case submitOrder(_ params: [String] = [])
    case payLoan(_ params: [String: Any] = [:])
    case updateUserInfo(_ params: [String: Any] = [:])
    case getBankCardList(_ params: [String: Any] = [:])
    case queryBankInfoByABMcode(_ params: [String: Any] = [:])
    case isCanApply(_ params: [String: Any] = [:])
    case queryVersion(_ params: [String: Any] = [:])
    case queryWhatsapp(_ params: [String: Any] = [:])
    case queryAutoPay(_ params: [String: Any] = [:])
    case checkUserInfoIsComplete(_ params: [String: Any] = [:])
    case checkUserAttachInfoIsComplete(_ params: [String: Any] = [:])
}
extension HomeNetType: DDTargetType {
    func buildBaseURL(_ req: [String : Any]) -> URL {
        switch self {
        case .getProxy:
            return URL(string: "http://www.inmediatoprestamoapp.com") ?? DDNetworkManager.shared.baseURL
        default:
            return DDNetworkManager.shared.baseURL
            
        }
    }
    
    func buildPath(_ req: [String : Any]) -> String {
        switch self {
        case .getProxy:
            return "/agregacion"
        case .getProducts:
            return "/user/v1/showReportInfo"
        case .getOrderInfo:
            return "/user/v1/showOrderInfo"
        case .updateUserInfo:
            return "/user/v1/updateUserInfo"
        case .getAutoPayInfo:
            return "/order/v1/query_auto_pay"
        case .createTmpOrder:
            return "/order/v1/create"
        case .removeTmpOrder:
            return "/order/v1/remove"
        case .submitOrder:
            return "/order/v1/submit"
        case .payLoan:
            return "/order/v1/payin"
        case .getBankCardList:
            return "/platform/v1/queryBankInfoAll"
        case .queryBankInfoByABMcode:
            return "/platform/v1/queryBankInfoByABMcode"
        case .isCanApply:
            return "/user/v1/isCanApply"
        case .queryVersion:
            return "/common/v1/queryVersion"
        case .queryWhatsapp:
            return "/common/v1/queryWhatsapp"
        case .queryAutoPay:
            return "/order/v1/queryAutoPay"
        case .checkUserInfoIsComplete:
            return "/user/v1/checkUserInfoIsComplete"
        case .checkUserAttachInfoIsComplete:
            return "/user/v1/checkUserAttachInfoIsComplete"
            
        default:
            return ""
        }
    }
    
    func buildMethod(_ req: [String : Any]) -> Moya.Method {
        switch self {
        case .getProxy,
            .getProducts,
            .getAutoPayInfo,
            .getBankCardList,
            .queryBankInfoByABMcode,
            .isCanApply,
            .getOrderInfo,
            .queryVersion,
            .queryWhatsapp,
            .queryAutoPay,
            .checkUserInfoIsComplete,
            .checkUserAttachInfoIsComplete:
            return .get
        default:
            return .post
        }
    }
    
    func buildTask(_ req: [String : Any]) -> Moya.Task {
        switch self {
        case .getProxy(let params),
            .queryVersion(let params),
            .queryWhatsapp(let params),
            .queryAutoPay(let params):
            return .requestParameters(parameters: req + params, encoding: URLEncoding.queryString)
        case .getProducts(let params),
            .getAutoPayInfo(let params),
            .getBankCardList(let params),
            .isCanApply(let params),
            .queryBankInfoByABMcode(let params),
            .checkUserInfoIsComplete(let params),
            .checkUserAttachInfoIsComplete(let params):
            var reqParams: [String: Any] = req + params
            reqParams["phoneNumber"] = KPUser.shared.phoneNumber
            return .requestParameters(parameters: reqParams, encoding: URLEncoding.queryString)
            
        case .getOrderInfo:
            let reqParams: [String: Any] = [
                "phoneNumber": KPUser.shared.phoneNumber
            ]
            return .requestParameters(parameters: req + reqParams, encoding: URLEncoding.queryString)
            
        case .updateUserInfo(let params),
             .removeTmpOrder(let params),
             .payLoan(let params):
            var reqParams: [String: Any] = params
            reqParams["phoneNumber"] = KPUser.shared.phoneNumber
            return .requestParameters(parameters: req + reqParams, encoding: JSONEncoding.default)
            
        case .createTmpOrder(let params):
            var reqParams: [String: String] = params as? [String: String] ?? [:]
            reqParams["phoneNumber"] = KPUser.shared.phoneNumber
            return .requestCustomJSONEncodable([reqParams], encoder: JSONEncoder())

        case .submitOrder(let params):
//            var reqParams: [String: String] = params as? [String: String] ?? [:]
//            let tmpReq: [String: String] = req.compactMapKeysAndValues({ (key, value) in
//                return (key, "\(value)")
//            })
//            reqParams["phoneNumber"] = KPUser.shared.phoneNumber
            return .requestCustomJSONEncodable(params, encoder: JSONEncoder())

        default:
            return .requestPlain
        }
    }
    
    func buildHeaders(_ req: [String : Any]) -> [String : String]? {
        return ["Authorization": "Bearer \(KPUser.shared.token)"]
    }
    
    var apiType: Int {
        switch self {
        case .getProxy:
            return Self.base + 1
        case .getProducts:
            return Self.base + 2
        case .getAutoPayInfo:
            return Self.base + 3
        case .getBankCardList:
            return Self.base + 4
        case .queryBankInfoByABMcode:
            return Self.base + 5
        case .isCanApply:
            return Self.base + 6
        case .getOrderInfo:
            return Self.base + 7
        case .queryVersion:
            return Self.base + 8
        case .queryWhatsapp:
            return Self.base + 9
        case .queryAutoPay:
            return Self.base + 10
        case .checkUserInfoIsComplete:
            return Self.base + 11
        case .checkUserAttachInfoIsComplete:
            return Self.base + 12
        case .createTmpOrder:
            return Self.base + 13
        case .removeTmpOrder(_):
            return Self.base + 14
        case .submitOrder(_):
            return Self.base + 15
        case .payLoan(_):
            return Self.base + 16
        case .updateUserInfo(_):
            return Self.base + 17
        }
    }
    
    
}
